package com.huluobo.a2308douyindemo.adapter

import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.huluobo.a2308douyindemo.R
import com.huluobo.a2308douyindemo.bean.MessageBean

/**
 *  Created by LC on 2025/11/3.
 */
class MessageAdapter : BaseQuickAdapter<MessageBean, BaseViewHolder>(R.layout.item_chat_room) {
    override fun convert(holder: BaseViewHolder, item: MessageBean) {
        val msg = "${item.username} : ${item.content}"

        //富文本编辑器
        val builder = SpannableStringBuilder(msg)

        val blue = ForegroundColorSpan(Color.BLUE)
        val red = ForegroundColorSpan(Color.RED)
        val yellow = ForegroundColorSpan(Color.YELLOW)

        if (item.username == "系统") {
            builder.setSpan(red, 0, item.username.length + 1, SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        } else {
            builder.setSpan(blue, 0, item.username.length + 1, SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        }

        builder.setSpan(yellow, item.username.length + 1, msg.length, SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)

        holder.setText(R.id.item_chat_room_tv, builder)
    }
}